#requires –version 2.0

function Test-Connections {
	param
(
		[Parameter(Position=0,Mandatory=$false)][string]$ComputerName,
		[Parameter(Mandatory=$false)][array]$List,
		[Parameter(Mandatory=$true)][string]$SubScripts,
		[Parameter(Mandatory=$false)][int]$MaxJobs = '25',
		[Parameter(Mandatory=$true)][string]$FailedAccessLogFullName,
		[Parameter(Mandatory=$true)][string]$ResultsTextFullName,
		[Parameter(Mandatory=$true)][string]$JobLogFullName,
		[Parameter(Mandatory=$false)][int]$TestTimeout = '60', # Seconds
		[Parameter(Mandatory=$false)][int]$JobmonTimeout = '600', # Seconds
		[Parameter(Mandatory=$true)][int]$TotalHosts,
		[Parameter(Mandatory=$false)][int]$DashCount,
		[Parameter(Mandatory=$false)][string]$ScriptTitle,
		[Parameter(Mandatory=$false)][boolean]$UseAltPCCredsBool = $false,
		[Parameter(Mandatory=$false)][string]$PCCreds,
		[Parameter(Mandatory=$false)][string]$wintitle_input
	)
	# VARIABLES
	[string]$Notes = ''
	[boolean]$Success = $false
	[datetime]$SubStartTime = Get-Date
	
	. "$SubScripts\Func_Get-Runtime_1.0.3.ps1"
	. "$SubScripts\Func_Test-Permissions_1.1.0.ps1"
	. "$SubScripts\Func_Add-HostToLogFile_1.0.4.ps1"
	. "$SubScripts\Func_Show-ScriptHeader_1.0.2.ps1"
		
	# REMOVE EXISTING OUTPUT PSOBJECT	
	if ($Global:TestConnections) {
		Remove-Variable TestConnections -Scope "Global"
	}
	
	#region Tasks
		
		if ($ComputerName) {
			[array]$List = $ComputerName
		}
		
		if ($List) {
			# SEND LIST TO COMPUTERS TO TEST C$ ACCESS
			Write-Host "`r".padright(40,' ') -NoNewline
			Test-Permissions -List $List -MaxJobs $MaxJobs -TestTimeout $TestTimeout -JobmonTimeout $JobmonTimeout -JobLogFullName $JobLogFullName -SubScripts $SubScripts -wintitle_input $Global:wintitle_input
			
			if ($Global:TestPermissions.Success -eq $true) {

				# IF FAILURES THEN UPDATE FAILED LOGS 
				##(Seperate from next statements so logs are wrote no matter next conditions results)
				if ($Global:TestPermissions.FailedCount -gt '0') {
					$Global:TestPermissions.FailedList | Foreach-Object {Add-HostToLogFile -ComputerName $_ -logfile $FailedAccessLogFullName}
					$FailedCount = $Global:TestPermissions.FailedCount
					$FailedList = $Global:TestPermissions.FailedList
				}

				# IF ALL FAILED REMOVE RESULTS FILE, UPDATE UI AND EXIT SCRIPT
				if ($Global:TestPermissions.AllFailed -eq $true) {
					# REMOVE EMPTY RESULTS FILE
					if ((Test-Path -Path $ResultsTextFullName) -eq $true) {
						Remove-Item -Path $ResultsTextFullName -Force
					}
					Show-ScriptHeader -blanklines '1' -DashCount $DashCount -ScriptTitle $ScriptTitle
					Write-Host "`r".padright(40,' ') -NoNewline
					Write-Host "`rERROR: ALL SYSTEMS FAILED PERMISSION TEST" -ForegroundColor White -BackgroundColor Red
					Write-Host ''
					[boolean]$Success = $true
					Break
				}
				# IF PARTIAL PASSED THEN PROMPT, REMOVE AND CONTINUE
				Elseif ($Global:TestPermissions.PartialPassed -eq $true) {
					[string]$Success = $true
					Show-ScriptHeader -blanklines '1' -DashCount $DashCount -ScriptTitle $ScriptTitle
					
					$FailedCount = $Global:TestPermissions.FailedCount
					$FailedList = $Global:TestPermissions.FailedList
					
					Write-Host "`r".padright(40,' ') -NoNewline
					Write-Host "`rCONNECTION ERROR TO THE FOLLOWING SYSTEMS" -ForegroundColor White -BackgroundColor Red
					Write-Host '-----------------------------------------'
					$FailedList | Select
					Write-Host '-----------------------------------------'
					Write-Host 'FAILED COUNT: ' -ForegroundColor Red -NoNewline
					Write-Host "($FailedCount)"
					Write-Host ''

					#region Prompt: Continue

						$title = ''
						$message = 'REMOVE ABOVE LIST OF HOSTS AND CONTINUE?'

						$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
						    'Continue with patching the available clients in the List.'

						$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
						    'Stop the script.'

						$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

						$result = $host.ui.PromptForChoice($title, $message, $options, 0) 

						switch ($result)
						{
						    0 {[boolean]$keepgoing = $true} 
						    1 {[boolean]$keepgoing = $false} 
						}
						if ($keepgoing -eq $false) {
							Write-Host ''
							Break
						}
					
					#endregion Prompt: Continue
				}
				# IF ALL PASSED THEN UPDATE UI AND CONTINUE
				Elseif ($Global:TestPermissions.AllPassed -eq $true) {
					Show-ScriptHeader -blanklines '1' -DashCount $DashCount -ScriptTitle $ScriptTitle
					Write-Host "`rPASSED PERMISSIONS TEST" -ForegroundColor Green -NoNewline
					Sleep -Seconds 2
					[boolean]$Success = $true
				}
				else
{
					# REMOVE EMPTY RESULTS FILE
					if ((Test-Path -Path $ResultsTextFullName) -eq $true) {
						Remove-Item -Path $ResultsTextFullName -Force
					}
					Show-ScriptHeader -blanklines '1' -DashCount $DashCount -ScriptTitle $ScriptTitle
					Write-Host "`r".padright(40,' ') -NoNewline
					Write-Host "`rERROR: PERMISSION TEST FAILURE" -ForegroundColor White -BackgroundColor Red
					Write-Host ''
					Break
				}
				
				# CREATE PASSED LIST OUTPUT
				[array]$passedList = $Global:TestPermissions.PassedList
				
				# COPY PASSED COUNT TO OUTPUT
				[int]$passedcount = $Global:TestPermissions.PassedCount
				
			} # IF Test-Permissions Successful
			else
{
				[string]$Notes += 'Test-Permissions function Failed '
			}
		} #/If Host not blank
		else
{
			[string]$Notes += 'Missing Hostname '
		}
		
	#endregion Tasks
	
	Get-Runtime -StartTime $SubStartTime
	
	# Create Results Custom PS Object
	$Global:TestConnections = New-Object -TypeName PSObject -Property @{
		Success = $Success
		Notes = $Notes
		Starttime = $SubStartTime
		Endtime = $Global:GetRuntime.Endtime
		Runtime = $Global:GetRuntime.Runtime
		PassedList = $passedList
		PassedCount = $passedcount
		FailedList = $FailedList
		FailedCount = $FailedCount
	}
}

#region Notes

<# Description
	function to test connection access to a remote Windows system.
#>

<# Author
	Levon Becker
	PowerShell.Guru@BonusBits.com
	http://wiki.bonusbits.com
#>

<# Dependents
	Check-WSUSClients
#>

<# Dependencies
	Func_Get-Runtime
	Func_Test-Permissions
#>

<# Change Log
	1.0.0 - 02/02/2012
		Created
	1.0.0 - 02/04/2012
		Added TotalHosts parameter
		Added Write-Progress
	1.0.1 - 04/20/2012
		Move Notes to end
		Renamed soem parameters
		Fixed strict var types that were string and supposed to be boolean
	1.0.2 - 04/27/2012
		Changes to make work in Module and not have default values.
	1.0.3 - 04/30/2012
		Added support for Alternate PC Credentials
	1.0.4 - 05/08/2012
		Added wintitle_input parameter to pass to Test-Permissions
	1.0.5 - 05/15/2012
		Added FailedList and FailedCount to Output
#>
<# TO DO
	1. Other testing sub functions can be added for various connection tests
		a. ping?
		b. vCenter PowerON?
		c. Port checks?
#>

#endregion Notes
